package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.ninetaildemonfox.base.BaseFragment;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.data.entity.SerializableList;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.ViewPagerAdp;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.fragment.adapter.PictureP2Fragment;
import com.ninetaildemonfox.zdl.socialproject.utils.MainViewPager;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;



/**
 * @author NineTailDemonFox
 * @date 2019/8/26 14:04
 * 功能描述：图片/视频预览
 * 联系方式：1037438704@qq.com
 */
public class PicturePreviewActivity2 extends BaseActivity implements PictureP2Fragment.onTestListener {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.viewPager)
    MainViewPager viewPager;
    private List<BaseFragment> fragments;
    private String cover_member_id;
    private PictureP2Fragment picturePFragment;
    private boolean isUp = true;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_picture_preview;
    }

    public static List<Map<String, String>> list;

    @Subscribe
    @Override
    public void onInitialization(Bundle bundle) {
        viewPager.setNoScroll(false);
        imageleftFinish.setVisibility(View.VISIBLE);
        fragments = new ArrayList<>();
        textCenter.setText("图片/视频预览");
        SerializableList serializableList = (SerializableList) getIntent().getExtras().getSerializable("serializableList");
        int position = getIntent().getExtras().getInt("position");
        cover_member_id = getIntent().getExtras().getString("cover_member_id");
        list = serializableList.getList();
        if (list == null || list.size() == 0) {
            finish();
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            picturePFragment = PictureP2Fragment.intentFragment(i, cover_member_id);
            fragments.add(picturePFragment);
        }
        ViewPagerAdp viewPagerAdp = new ViewPagerAdp(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(viewPagerAdp);
        viewPager.setCurrentItem(position);
    }

    @OnClick({R.id.image_left_finish, R.id.text_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                JZVideoPlayer.releaseAllVideos();
                finish();
                break;
            default:
        }
    }

    public static void start(Context me, List<Map<String, String>> list, int position, int type, String cover_member_id) {
        Intent intent = new Intent(me, PicturePreviewActivity2.class);
        Bundle bundle = new Bundle();
        SerializableList serializableList = new SerializableList(list);
        bundle.putSerializable("serializableList", serializableList);
        bundle.putInt("position", position);
        bundle.putInt("type", type);
        bundle.putString("cover_member_id", cover_member_id);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onTest(boolean xuanze) {
//        ToastUtil.show("" + xuanze);
        viewPager.setNoScroll(xuanze);
    }
}